package com.skit.main.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.skit.main.R;


/**
 * @author shuike <a href="mailto:shuike007@gmail.com">Contact me.</a>
 * @version 1.0
 * @since 2020/6/1
 */
public class EditBottomSheetFragment extends BottomSheetDialogFragment {
    private View view;

    private Handler handler = new Handler();
    private DoneListener doneListener;

    public interface DoneListener {
        void done(String s);
    }

    public void setDoneListener(DoneListener doneListener) {
        this.doneListener = doneListener;
    }

    public static EditBottomSheetFragment newInstance(String data) {

        Bundle args = new Bundle();
        args.putString("data", data);

        EditBottomSheetFragment fragment = new EditBottomSheetFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, R.style.BottomSheetDialogStyle);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.main_bottom_sheet_layout, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Button submit = view.findViewById(R.id.submit);
        EditText editText = view.findViewById(R.id.editText);
        editText.setHint(getTag());
        String data = getArguments().getString("data", "");
        editText.setText(data);
        submit.setOnClickListener(v -> {
            if (doneListener != null) {
                doneListener.done(editText.getText().toString());
            }
            dismiss();
        });
    }
    
    private void toast(String s) {
        Toast.makeText(getActivity(), s, Toast.LENGTH_SHORT).show();
    }
}
